//
//  HNCreditGoidHaiZi.m
//  XDEducation
//
//  Created by 星道 on 17/6/1.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNCreditGoidHaiZi.h"
#import "HNCreditGoldSelectView.h"
#import "HNCreditGoldController.h"

@implementation HNCreditGoidHaiZi
{
    UIViewController *_VC;
}


- (instancetype)init_GetHaiInfoWithVC:(UIViewController *)VC
{
    self = [super init];
    if (self)
    {
        _VC = VC;
    }
    return self;
}



- (void)SelectRowWithTitle:(NSString *)Title
{
    
    if ([Title isEqualToString:@"老师"])
    {
        [self Teacher];
    }
    else if ([Title isEqualToString:@"家长"])
    {
        [self Family];
    }
    else if ([Title isEqualToString:@"自己"])
    {
        [self Own];
    }
    else
    {
    	}
}



#pragma mark - 老师
- (void)Teacher
{
    //打开所有老师
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];
    [self RefreshDataWithUrlStr:@"qun.do?action=getqun" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.invite_uid} Complete:^(id responseObj) {
        NSMutableArray *Arr =[NSMutableArray array];
        NSMutableArray *Title_Arr = [NSMutableArray array];
        for (NSDictionary *Dict in responseObj[@"qusers"])
        {
            if ([Dict[@"petName"] isKindOfClass:[NSNull class]])
            {
                continue;
            }
            
            if (![Dict[@"rolecode"] isEqualToString:@"2"])
            {
                continue;
            }
            
            [Arr addObject:Dict];
            NSString *PetName = Dict[@"petName"];
            [Title_Arr addObject:PetName];
        }
        
            HNCreditGoldSelectView *SelectView = [[HNCreditGoldSelectView alloc] initWithTitle_Arr:Title_Arr SelectRow:^(NSString *Title) {
                
                //获取次级老师的数据后打开我的约定
                for (NSDictionary *Dict in Arr)
                {
                    if ([Dict[@"petName"] isEqualToString:Title])
                    {
                        [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":Dict[@"uid"]} Complete:^(id responseObj) {
                            
                            //爱心荣耀努力合约
                            if (![Dict[@"avatar"] isKindOfClass:[NSNull class]])
                            {
                                VC.Image = Dict[@"avatar"];
                            }
                            VC.Love = responseObj[@"t"][@"redHearts"];
                            VC.Glory = responseObj[@"t"][@"stars"];
                            VC.Efforts = responseObj[@"t"][@"flowers"];
                            VC.Contract = responseObj[@"t"][@"honests"];
                            
                            [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"supervisorUid":Dict[@"uid"],@"executeUid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                                
                                //小目标
                                VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
                                VC.Small_Finish = responseObj[@"t"][@"finished"];
                                
                                [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"supervisorUid":Dict[@"uid"],@"executeUid":[AccountTool shareAccountTool].currentAccount.uid,@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                                    
                                    //大目标
                                    VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                                    VC.Big_Finish = responseObj[@"t"][@"finished"];
                                    VC.uid = [Dict[@"uid"] string];
                                    VC.executeUid = @([[AccountTool shareAccountTool].currentAccount.uid integerValue]);
                                    VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeLaoShi;
                                    [_VC.navigationController pushViewController:VC animated:YES];
                                }];
                            }];
                        }];
                        break;
                    }
                }
            }];
            [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:SelectView];
        }];

}
#pragma mark - 自己
- (void)Own
{
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];

    [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
       
        VC.Image = ([AccountTool shareAccountTool].currentAccount.avatar);
        VC.Love = responseObj[@"t"][@"redHearts"];
        VC.Glory = responseObj[@"t"][@"stars"];
        VC.Efforts = responseObj[@"t"][@"flowers"];
        VC.Contract = responseObj[@"t"][@"honests"];
        
        [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"executeUid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
            
            //小目标
            VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
            VC.Small_Finish = responseObj[@"t"][@"finished"];
            
            [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                
                //大目标
                VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                VC.Big_Finish = responseObj[@"t"][@"finished"];
                VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeZiJi;
                [_VC.navigationController pushViewController:VC animated:YES];
            }];
        }];
    }];
}

#pragma mark - 家长
- (void)Family
{
    HNCreditGoldController *VC = [[HNCreditGoldController alloc] init];
    //1、首先获取用户头像接口
    [self RefreshDataWithUrlStr:@"qun.do?action=getqun" Params:@{@"uid":[AccountTool shareAccountTool].currentAccount.invite_uid} Complete:^(id xx) {
        
        NSDictionary *Obj = [xx[@"qusers"] firstObject];
        if (![Obj[@"avatar"] isKindOfClass:[NSNull class]])
        {
            VC.Image = (Obj[@"avatar"]);
        }
        NSString *FamilyID = [Obj[@"uid"] stringValue];
        //2、获取顶部数据
        [self RefreshDataWithUrlStr:@"user.do?action=getHonestValues" Params:@{@"uid":FamilyID} Complete:^(id responseObj) {
            VC.Love = responseObj[@"t"][@"redHearts"];
            VC.Glory = responseObj[@"t"][@"stars"];
            VC.Efforts = responseObj[@"t"][@"flowers"];
            VC.Contract = responseObj[@"t"][@"honests"];
            
            [self RefreshDataWithUrlStr:@"user.do?action=getTargetStatus2" Params:@{@"supervisorUid":FamilyID,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                
                //小目标
                VC.Small_NoFinish = responseObj[@"t"][@"notFinished"];
                VC.Small_Finish = responseObj[@"t"][@"finished"];
                
                [self RefreshDataWithUrlStr:@"user.do?action=getBigTargetStatus2" Params:@{@"supervisorUid":FamilyID,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid,@"uid":[AccountTool shareAccountTool].currentAccount.uid} Complete:^(id responseObj) {
                    
                    //大目标
                    VC.Big_NoFinish = responseObj[@"t"][@"notFinished"];
                    VC.Big_Finish = responseObj[@"t"][@"finished"];
                    VC.uid = [[AccountTool shareAccountTool].currentAccount.invite_uid stringValue];
                    VC.HNCreditGoldControllerAccoutType = HNCreditGoldControllerAccoutTypeJiaZhang;
                    [_VC.navigationController pushViewController:VC animated:YES];
                }];
            }];

        }];
    }];
}



+ (NSArray *)GetHaiZiInfo
{
    return @[@"家长",@"自己",@"老师"];
}

- (void)RefreshDataWithUrlStr:(NSString *)UrlStr Params:(NSDictionary *)Params Complete:(void(^)(id))Complete
{
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {
        

        Complete (responseObj);
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}
@end
